package com.diligent.cloud.sfc.resources.impl;

import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.canny.commons.page.IPage;
import com.diligent.cloud.sfc.model.params.OrderEndParams;
import com.diligent.cloud.sfc.model.params.OrderListParams;
import com.diligent.cloud.sfc.model.params.OrderStartParams;
import com.diligent.cloud.sfc.resources.WoOrderResource;
import com.diligent.cloud.sfc.service.WoOrderService;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.web.resource.impl.AdaptationBaseResourceImpl;
import com.diligent.cloud.workorder.enums.WoOrderStatus;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.model.WoOrderProgressParams;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.foundation.web.action.DiligentActionFunction;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * WoOrderResourceImpl class
 *
 * @author xf107278
 * @date 3/28/2020
 */
@RestController
public class WoOrderResourceImpl extends AdaptationBaseResourceImpl<WoOrder> implements WoOrderResource {

    private WoOrderService orderService;
    private final WoOrderFeignService orderFeignService;

    public WoOrderResourceImpl(WoOrderService orderService, WoOrderFeignService orderFeignService) {
        this.orderService = orderService;
        this.orderFeignService = orderFeignService;
        init();
    }

    @Override
    public ResponseEntity<Result> getOrderAcInfo(Long orderId, Integer stationType, Principal principal) {
        return orderService.getOrderAcInfo(orderId, stationType, principal);
    }


    @Override
    public ResponseEntity<Result> addOrder(WoOrder woOrder, Principal principal) {
        return orderService.addOrder(woOrder, principal);
    }

    @Override
    public ResponseEntity<ListResult> getOrderList(OrderListParams orderListParams, Principal principal) {
        return orderService.getOrderList(orderListParams);
    }


    @Override
    public ResponseEntity<ListResult> getSameOrderByProdBatch(String prodBatch, String machCode, Integer orderType, Long systemId, Integer isFinished, Integer operationIsFinished) {
        return orderService.getSameOrderByProdBatch(prodBatch, machCode, systemId, orderType, isFinished,operationIsFinished);
    }

//    @Override
//    public ResponseEntity<ObjectResult> addWoOrderLots(String boxCode, Long systemId, Principal principal) {
//        return orderService.addWoOrderLots(boxCode,systemId,principal);
//    }

    @Override
    public ResponseEntity<com.diligent.cloud.primary.data.model.Result> getWoOrderProgress(WoOrderProgressParams orderProgressParams) {
        return orderService.getWoOrderProgress(orderProgressParams);
    }

    @Override
    public ResponseEntity<com.diligent.cloud.primary.data.model.Result> updateDocStatus(String action, WoOrder woOrder, Principal principal) {
        return orderService.updateOrderDocStatus(action, woOrder, principal);
    }

    @Override
    public ResponseEntity<ObjectResult> getOrderByPageAndCondition(PageAndCondition pageAndCondition) {
        ResponseEntity<com.diligent.cloud.primary.data.model.Result<IPage<WoOrder>>> page =
                orderFeignService.page(pageAndCondition);
        return ResponseEntity.ok(new ObjectResult<>(page.getBody().getData()));
    }



    @Override
    public ResponseEntity<ObjectResult<WoOrder>> selectByCode(String code) {
        return null;
    }

    @Override
    public ResponseEntity<ObjectResult<WoOrder>> selectById(Long id) {
        return orderService.selectById(id);
    }

    private void init() {
        DiligentActionFunction diligentActionFunction = (var1, var2, var3) -> {
            OrderStartParams var31 = (OrderStartParams) var3;

            return orderService.updateOrderStatus(var31.getOrderId(), var31.getMdId(),var31.getAsId(), var31.getProdBatch(),var31.getPrincipal()).getBody();
        };

        DiligentActionFunction orderEndActionFunction = (var1, var2, var3) -> {
            OrderEndParams var31 = (OrderEndParams) var3;
            return orderService.updateOrderStatus(WoOrderStatus.finish.getStatus(), var31.getOrderId(),var31.getAsId(),var31.getPrincipal()).getBody();
        };

        super.registerAction(HttpMethod.POST.toString(), "start", OrderStartParams.class, diligentActionFunction);

        super.registerAction(HttpMethod.POST.toString(), "end", OrderEndParams.class, orderEndActionFunction);

    }
}
